<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\cache\driver\Redis;

class Auth extends Controller
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $urldata = $request->param();
        // if(isset($urldata['createadmin_id']) && $urldata['createadmin_id']){
        //     //问题反馈列表跳转的
        //     session('loginid',$urldata['createadmin_id']);
        //     $admin_id=session('loginid');
        // }else{
        //      $admin_id=session('userid');
        // }
        $admin_id = session('userid');
        //获取相关的配置的信息
        //$this->assign(getConfig());
        $admin = Db::name('admin')->alias('a')->join('nt_admin_info b', 'a.id=b.admin_id', 'left')->field('a.*,b.firstname,b.lastname,b.birthday,b.remarks,b.sex,b.marry,b.wage,b.education')->where('a.id', $admin_id)->find();
        if ($admin) {
            session('admin', $admin);//存储的是用户的信息
//            $redis = new \Redis();
//            $redis->connect('127.0.0.1',6379);
//            $redis->auth("huxing");
//            $session_userid = $redis->get("userid".$admin['id']);//登录用户的sessionid
//            if($session_userid == session_id()){
//                $redis->set("logintime".$admin['id'],time());
//                session('admin', $admin);//存储的是用户的信息
//            }else{
//                session(null);
//                echo "<script>alert('该账号在其他地方已经登陆');location.href='/admin/login/logout';</script>";exit;
//            }
        } else {
            if ($request->isGet()) {
                echo "<script>;location.href='/admin/login/logout';</script>";
                exit;
            }
        }
        if (isset($this->data['lang_type']) && $this->data['lang_type']) {
            $this->lang = $this->data['lang_type'];
        } else {
            $this->lang = is_lang();
        }
        //检测权限
        $this->checkauth();
        //菜单权限
        $this->getauth($urldata);
    }

    //获取当前登录用户的菜单权限
    protected function getauth($data)
    {
        //获取当前用户登录的角色id
        $admin = session('admin');
        $position_id = $admin['position_id'];
        //管理员 CEO
        $mapmenu['zu.is_menu'] = 1;
        $mapmenu['zu.disable'] = 0;
        $mapmenu['mu.lang'] = is_lang();
        $field = ['zu.*,mu.content'];
        if ($position_id == 1) {
            //是否展示 是否停用 菜单权限
            $menu = getTreeDatas('menu', [['multi_lang mu', 'zu.mkey = mu.mkey']], $field, $mapmenu);
        } else {
            //非管理员，ceo
            $manage_auth = $admin['auth_ids'];
            $mapmenu['zu.id'] = array('in', $manage_auth);
            $menu = getTreeDatas('menu', [['multi_lang mu', 'zu.mkey = mu.mkey']], $field, $mapmenu);
        }
        if (isset($data['formMenu'])) {
            $formMenu = 1;
        } else {
            $formMenu = 2;
        }
        $this->assign([
            'menu' => $menu,//菜单栏的显示
            'adminauth_ids' => viewAuth(),//访问页面的权限ids
            'langs' => getlangnames(),
            'formMenu' => $formMenu,
            'controller' => request()->controller(),
            'action' => request()->action(),
        ]);
    }

    //防止用户手动改变url地址
    protected function checkauth()
    {
        $admin = session('admin');
        if (!$admin) {
            echo json_encode(array('msg' => '你已登陆超时，请重新登陆', 'status' => 5));
            exit;
        }
        //获取当前用户的角色id
        $position_id = $admin['position_id'];
        //获取这个用户的个人权限id
        $auth_manage = explode(',', $admin['auth_ids']);
        $controller = request()->controller();
        $action = request()->action();

//        $mapuserright['disable'] =  $rightmap['disable'] = 0;
//        $mapuserright['control'] =  $rightmap['control'] = strtolower($controller);
//        $mapuserright['is_right'] = $rightmap['is_right'] = 1;
//        $mapuserright['id'] = ['in', $auth_manage];

//        //右侧按钮
//        $right = Db::name('menu')->where($rightmap)->field('control,option,nameclass,mkey')->select();
//        foreach ($right as $rightkey => $rightval) {
//            $right[$rightkey]['content'] = getlangname('multi_lang', ['mkey' => $rightval['mkey']], $this->lang);
//        }
//
////        //个人右侧按钮
//        $userright = Db::name('menu')->where($mapuserright)->field('control,option,nameclass,mkey')->select();
//        foreach ($userright as $userrightkey => $userrightval) {
//            $userright[$userrightkey]['content'] = getlangname('multi_lang', ['mkey' => $userrightval['mkey']], $this->lang);
//        }
//        if ($position_id == 1) {
//            //返回这个页面的所有按钮
//            $this->btnall = $right;
//        }else{
//            $this->btnall = $userright;
//        }
        //管理员 ceo不需要检测
        if ($position_id == 1) {
            return;
        }
        //获取控制器与方法
        if ($controller == 'Index' && $action == 'index') {
            //首页不要检测
            return;
        }
        //当前页面，对应的权限
        $auth = Db::name('menu')->where([
            'control' => strtolower($controller),
            'option' => strtolower($action),
            'disable' => 0//是否停用
        ])->find();
        //判断用户的个人权限did
        if (!in_array($auth['id'], $auth_manage)) {
            if (request()->isAjax() && request()->isPost()) {
                //表示没有权限
                echo json_encode(array('msg' => lang("no_permission,_please_contact_administrator"), 'status' => 3,));
                exit;
            } elseif (request()->isGet()) {
                //表示没有权限
                echo lang("no_permission,_please_contact_administrator");
                exit;
            } else {
                echo lang("no_permission,_please_contact_administrator");
                exit;
            }
        }
        return;
    }

    // 获取当前语言类型
    public function getLang($lang_type = '')
    {
        if ($lang_type && $lang_type) {
            return is_lang($lang_type);
        } else {
            return is_lang();
        }
    }


    // 获取redis 中的数据
    public function getRedisData($keyName)
    {
        if ($this->redis->get($keyName)) {
            return $this->redis->get($keyName);
        } else {
            return '';
        }
    }


    // 设置redis 数据
    public function setRedisData($keyName, $value)
    {
        if ($this->redis->set($keyName, $value)) {
            return true;
        } else {
            return false;
        }
    }

    //错误提示页面
    public function _empty()
    {
        return view('index/mistake');
    }

}